package com.csun.cmny.provider.controller;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.pojo.CacBigItem;
import com.csun.cmny.provider.pojo.CacMinorItem;
import com.csun.cmny.provider.service.CacBigItemService;
import com.csun.cmny.provider.service.CacMinorItemService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
 * 评估大项管理——机构额外大项
 * */
@RestController
@RequestMapping("/CacBigItem")
public class CacBigItemController extends BaseController {
    @Autowired
    private CacBigItemService cacBigItemService;
    @Autowired
    private CacMinorItemService cacMinorItemService;

    /*
     * 这里的认证和当前登录的用户注解先预留，等完善了再进行修改
     * 1.判断该用户是否属于该机构人员
     * */
    //添加单个大项
    @RequestMapping(value = "/addCacBigItem", method = RequestMethod.POST)
    public Wrapper<CacBigItem> addCacType(CacBigItem cacBigItem) {
        //校验
        if (cacBigItem.getName() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项名称不能为空", null);
        }
        if (cacBigItem.getCacTypeId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别id不能为空", null);
        }
        if (cacBigItem.getType() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别不能为空", null);
        }
        cacBigItem.setGroupId(getLoginAuthDto().getGroupId());//机构组织ID
        CacBigItem cacBigItem1 = new CacBigItem();
        cacBigItem1.setCacTypeId(cacBigItem.getCacTypeId());
        cacBigItem1.setName(cacBigItem.getName());
        cacBigItem1.setType(cacBigItem.getType());
        cacBigItem1.setGroupId(getLoginAuthDto().getGroupId());//机构组织ID
        //根据大项名称、类别id和类别判断该大项是否在该类别中已经存在
        CacBigItem cacBigItem2 = cacBigItemService.selectByCbi(cacBigItem1);
        if (cacBigItem2 != null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该大项已经在该类别中存在", null);
        }

        int n = cacBigItemService.insert(cacBigItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败", cacBigItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功", cacBigItem);
    }

    //添加单个大项——批量新增小项
    @RequestMapping(value = "/addCacBigItems", method = RequestMethod.POST)
    public Wrapper addCacBigItems(@RequestBody CacBigItem cacBigItem) {
        //校验
        if (cacBigItem.getName() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项名称不能为空", null);
        }
        if (cacBigItem.getCacTypeId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别id不能为空", null);
        }
        if (cacBigItem.getType() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别不能为空", null);
        }
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Long modifier = loginAuthDto.getUserId();
        cacBigItem.setGroupId(getLoginAuthDto().getGroupId());//机构组织ID
        CacBigItem cacBigItem1 = new CacBigItem();
        cacBigItem1.setGroupId(getLoginAuthDto().getGroupId());//机构组织ID
        cacBigItem1.setCacTypeId(cacBigItem.getCacTypeId());
        cacBigItem1.setName(cacBigItem.getName());
        cacBigItem1.setType(cacBigItem.getType());
        //根据大项名称、类别id和类别判断该大项是否在该类别中已经存在
        CacBigItem cacBigItem2 = cacBigItemService.selectByCbi(cacBigItem1);
        if (cacBigItem2 != null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该大项已经在该类别中存在", null);
        }
        cacBigItem.setModifier(modifier);
        int n = cacBigItemService.insert(cacBigItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "添加失败", cacBigItem);
        }
        //批量新增小项
        List<CacMinorItem> cacMinorItemList = cacBigItem.getCacMinorItemList();
        if (!CollectionUtils.isEmpty(cacMinorItemList)) {
            //校验
            for (int i = 0; i < cacMinorItemList.size(); i++) {
                if (org.springframework.util.StringUtils.isEmpty(cacMinorItemList.get(i).getName())) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项类别名称不能为空", cacMinorItemList.get(i));
                }
                if (cacBigItem.getType() == 0 && cacMinorItemList.get(i).getGrade() == null) {
                    return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "小项分数不能为空", cacMinorItemList.get(i));
                }
                for (int j = 0; j < cacMinorItemList.size(); j++) {
                    if (j != i && cacMinorItemList.get(i).getName().equals(cacMinorItemList.get(j).getName())) {
                        return WrapMapper.wrap(Wrapper.ERROR_CODE, "小项类别名称不能相同", null);
                    }
                    cacMinorItemList.get(i).setBigItemId(cacBigItem.getBigItemId());
                    cacMinorItemList.get(i).setModifier(modifier);
                    cacMinorItemList.get(i).setGroupId(getLoginAuthDto().getGroupId());//添加组织机构ID
                }
            }
            //批量插入
            int m = cacMinorItemService.insertBatch(cacMinorItemList);
            if (m <= 0) {
                System.out.println("********************新增大项时批量插入小项失败********************");
            }
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "添加成功", cacBigItem);
    }

    //根据大项id删除大项及大项下的小项
    @RequestMapping(value = "/delCacBigItem", method = RequestMethod.POST)
    public Wrapper delCacType(Integer bigItemId) {
        if (bigItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        //获取当前用户的机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();

        //判断当前登录用户是否是该机构人员

        //删除大项下面的小项
        int m = cacMinorItemService.delByBid(bigItemId);

        //删除大项
        int n = cacBigItemService.delById(bigItemId);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "删除失败", n);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "删除成功", n);
    }

    //修改单个大项
    @RequestMapping(value = "/updateCacBigItem", method = RequestMethod.POST)
    public Wrapper<CacBigItem> updateCacType(CacBigItem cacBigItem) {
        if (cacBigItem.getBigItemId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        if (cacBigItem.getCacTypeId() == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "类别id不能为空", null);
        }
        //判断当前登录用户是否是该机构人员

        //获取机构id
        LoginAuthDto loginAuthDto = getLoginAuthDto();
        Integer homeId = loginAuthDto.getGroupId().intValue();
        cacBigItem.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        CacBigItem cacBigItem1 = new CacBigItem();
        cacBigItem1.setCacTypeId(cacBigItem.getCacTypeId());
        cacBigItem1.setName(cacBigItem.getName());
        cacBigItem1.setGroupId(getLoginAuthDto().getGroupId());//添加组织ID
        //根据大项名称和类别d判断该小项是否在该大项中已经存在
        CacBigItem cacBigItem2 = cacBigItemService.selectByCbi(cacBigItem1);
        if (cacBigItem2 != null && !cacBigItem2.getBigItemId().equals(cacBigItem.getBigItemId())) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "该大项已经在该类别中存在", cacBigItem2);
        }

        int n = cacBigItemService.update(cacBigItem);
        if (n <= 0) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "修改失败", cacBigItem);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "修改成功", cacBigItem);
    }

    //根据大项id查询单个大项
    @RequestMapping(value = "/selectCbiById", method = RequestMethod.POST)
    public Wrapper selectCbiById(Integer bigItemId) {
        if (bigItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        CacBigItem cacBigItem = cacBigItemService.selectCbiById(bigItemId);
        if (cacBigItem == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", bigItemId);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "查询成功", cacBigItem);
    }

    //根据大项id查询单个大项及大项下的小项
    @RequestMapping(value = "/selectById", method = RequestMethod.POST)
    public Wrapper selectById(Integer bigItemId) {
        if (bigItemId == null) {
            return WrapMapper.wrap(Wrapper.ILLEGAL_ARGUMENT_CODE_, "大项id不能为空", null);
        }
        CacBigItem cacBigItem = cacBigItemService.selectById(bigItemId);
        if (cacBigItem == null) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, "没有此id", bigItemId);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "查询成功", cacBigItem);
    }
}
